//
//  Utils.m
//  WeddingStoreFinder
//
//  Created by Macdev on 11/20/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Utils.h"
#include <sys/sysctl.h>

@implementation Utils


+ (float) getDistanceFromXCoordinate: (float) fromX froYCoordinate: (float) fromY toXCordinate: (float) toX toYCoordinate: (float) toY {
	//[Utils logString:[NSString stringWithFormat:@"fromX: %f, fromY: %f, toX: %f, toY: %f", fromX, fromY, toX, toY]];
	float earthRadian = 6371000; //m
	float lat1 = [Utils degreesToRadians:fromY]; 
	float lat2 = [Utils degreesToRadians:toY];
	float lon1 = [Utils degreesToRadians:fromX];
	float lon2 = [Utils degreesToRadians:toX];
	float retValue = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon2-lon1)) * earthRadian;
	return retValue;
}

+ (float) degreesToRadians:(float) degrees {
	return degrees * M_PI / 180;
}

+ (float) radianToDegrees:(double) radian {
	return radian * 180 / M_PI;
}

+ (NSString*) formatDistance:(double)distance {
	NSString *retValue;
	if (distance < 1000) {
		if (distance < 10) {
			retValue = @"0 m";
		} else {
			distance = round(distance/10)*10;
			retValue = [NSString stringWithFormat:@"%1.0f m", distance];
		}
	} else {
		distance = round(distance/100)/10;
		retValue = [NSString stringWithFormat:@"%1.1f km", distance];;
	}
	
	
	return retValue;
}

+ (NSString*) formatCurrency:(double)amount {
	NSNumber *number = [[NSNumber alloc] initWithDouble:amount];
	NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
	[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
	NSString *retValue = [formatter stringFromNumber:number];
	retValue = [retValue stringByReplacingOccurrencesOfString:@"," withString:@"."];
	retValue = [retValue stringByReplacingOccurrencesOfString:@" " withString:@"."];
	retValue = [retValue stringByAppendingString:@",- Ft"];
	[number release];
	[formatter release];
	return retValue;
}

+ (NSString *) platformName {
	size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platformString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
	
	NSArray *platformArray = [platformString componentsSeparatedByString:@","];
	
	if ([platformArray count] > 0) {
		NSString *platform = [platformArray objectAtIndex:0];
		if ([platform isEqualToString:@"iPhone1"]) {
			return @"iPhone 1-3G"; //@"iPhone 1-3G";
		} else if ([platform isEqualToString:@"iPhone2"]) {
			return @"iPhone 3GS"; //@"iPhone 3GS";
		} else if ([platform isEqualToString:@"iPhone3"]) {
			return @"iPhone 4"; //@"iPhone 4";
		} else if ([platform isEqualToString:@"iPhone4"]) {
			return @"iPhone 5"; //@"iPhone 5 ?";
		} else if ([platform isEqualToString:@"iPod1"]) {
			return @"iPod Touch 1G"; //@"iPod Touch 1G";
		} else if ([platform isEqualToString:@"iPod2"]) {
			return @"iPod Touch 2G"; //@"iPod Touch 2G";
		} else if ([platform isEqualToString:@"iPod3"]) { 
			return @"iPod Touch 3G"; //@"iPod Touch 3G";
		} else if ([platform isEqualToString:@"iPod4"]) {
			return @"iPod Touch 4G"; //@"iPod Touch 4G";
		} else if ([platform isEqualToString:@"iPod5"]) {
			return @"iPod Touch 5G"; //@"iPod Touch 5G ?";
		} else if ([platform isEqualToString:@"iPad1"]) {
			return @"iPad 1G"; //@"iPad 1G";
		} else if ([platform isEqualToString:@"iPad2"]) {
			return @"iPad 2G"; //@"iPad 2G";
		} else if ([platform isEqualToString:@"i386"]) {
			return @"iPhone Simulator"; //@"iPhone Simulator";
		} else if ([platform isEqualToString:@"x86_64"]) {
			return @"iPhone Simulator"; //@"iPhone Simulator";
		} else {
			return platform;
		}
		
	} else {
		return @"Unknown";
	}
}

+ (int) platformType {
	size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platformString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
	
	NSArray *platformArray = [platformString componentsSeparatedByString:@","];
	
	if ([platformArray count] > 0) {
		NSString *platform = [platformArray objectAtIndex:0];
		if ([platform isEqualToString:@"iPhone1"]) {
			return kPlatform_iPhone; //@"iPhone 1-3G";
		} else if ([platform isEqualToString:@"iPhone2"]) {
			return kPlatform_iPhone; //@"iPhone 3GS";
		} else if ([platform isEqualToString:@"iPhone3"]) {
			return kPlatform_iPhone; //@"iPhone 4";
		} else if ([platform isEqualToString:@"iPhone4"]) {
			return kPlatform_iPhone; //@"iPhone 5 ?";
		} else if ([platform isEqualToString:@"iPod1"]) {
			return kPlatform_iPod; //@"iPod Touch 1G";
		} else if ([platform isEqualToString:@"iPod2"]) {
			return kPlatform_iPod; //@"iPod Touch 2G";
		} else if ([platform isEqualToString:@"iPod3"]) { 
			return kPlatform_iPod; //@"iPod Touch 3G";
		} else if ([platform isEqualToString:@"iPod4"]) {
			return kPlatform_iPod; //@"iPod Touch 4G";
		} else if ([platform isEqualToString:@"iPod5"]) {
			return kPlatform_iPod; //@"iPod Touch 5G ?";
		} else if ([platform isEqualToString:@"iPad1"]) {
			return kPlatform_iPad; //@"iPad 1G";
		} else if ([platform isEqualToString:@"iPad2"]) {
			return kPlatform_iPad; //@"iPad 2G";
		} else if ([platform isEqualToString:@"i386"]) {
			return kPlatform_iPhoneSimulator; //@"iPhone Simulator";
		} else if ([platform isEqualToString:@"x86_64"]) {
			return kPlatform_iPhoneSimulator; //@"iPhone Simulator";
		} else {
			return kPlatform_Unknown;
		}
		
	} else {
		return kPlatform_Unknown;
	}
}

@end
